#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QStyle>
#include <QFileDialog>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建QMediaPlayer对象
    mediaplay = new QMediaPlayer(this);

    //创建放置视频的控件
    QVideoWidget * videoWidget = new QVideoWidget(this);

    //创建布局管理器
    QHBoxLayout * hLayout = new QHBoxLayout;
    //创建两个按钮
    QPushButton * chooseButton = new QPushButton("选择视频");
    QPushButton * playButton = new QPushButton("播放视频");
    QPushButton * pauseButton = new QPushButton("暂停视频");
    //设置按钮的尺寸策略
    chooseButton->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    playButton->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    pauseButton->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    playButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));

    //将按钮添加进布局管理器
    hLayout->addWidget(chooseButton);
    hLayout->addWidget(playButton);
    hLayout->addWidget(pauseButton);

    //创建布局管理器
    QVBoxLayout * vLayout = new QVBoxLayout(this);
    //添加进布局管理器
    vLayout->addWidget(videoWidget);
    //布局管理器添加布局管理器
    vLayout->addLayout(hLayout);
    //设置拉伸比例
    vLayout->setStretch(0,5);
    vLayout->setStretch(1,1);

    //绑定信号槽 ，选择视频按钮
    connect(chooseButton,&QPushButton::clicked,this,[=](){
        const QString &path = QFileDialog::getOpenFileName(this,"选择视频文件",".","video files(*.avi *.mp4 *.wmv)");

        //设置视频源
        mediaplay->setMedia(QUrl(path));
        //设置播放视频的控件
        mediaplay->setVideoOutput(videoWidget);
    });

    //播放视频
    connect(playButton,&QPushButton::clicked,this,[=](){
        mediaplay->play();
    });

    //暂停视频
    connect(pauseButton,&QPushButton::clicked,this,[=](){
        mediaplay->pause();
    });

}

Widget::~Widget()
{
    delete ui;
}

